//
//  WMEFirstTwoPageCollectionViewCell.m
//  WMEMusic
//
//  Created by 恩叔 on 16/4/12.
//  Copyright © 2016年 enshu. All rights reserved.
//

#import "WMEFirstTwoPageCollectionViewCell.h"
#import "WMEOneTableViewCell.h"
#import "WMEFirstPageModel.h"
#import "WMEHotListViewController.h"

@interface WMEFirstTwoPageCollectionViewCell ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong)UITableView *tableView;

@property (nonatomic, strong)NSMutableArray *dataArr;
@property (nonatomic, strong)NSMutableArray *contentArr;
//@property (nonatomic, strong)
@end
@implementation WMEFirstTwoPageCollectionViewCell
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        WS(ws);
        self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) style:UITableViewStylePlain];
        [self.contentView addSubview:_tableView];
        [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(ws.mas_left).with.offset(0);
            make.right.equalTo(ws.mas_right).with.offset(0);
            make.bottom.equalTo(ws.mas_bottom).with.offset(0);
            make.top.equalTo(ws.mas_top).with.offset(0);
        }];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerClass:[WMEOneTableViewCell class] forCellReuseIdentifier:@"oneTBVCell"];
        [self data];
    }
    return self;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_contentArr.count) {
        WMEOneTableViewCell *oneTBVCell = [_tableView dequeueReusableCellWithIdentifier:@"oneTBVCell" forIndexPath:indexPath];
        WMEFirstPageModel *model = _dataArr[indexPath.row];
        NSArray *arr = _contentArr[indexPath.row];
        oneTBVCell.model = model;
        oneTBVCell.array = arr;
        return oneTBVCell;
    }
    return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_dataArr.count) {
        return 100;
    }
    return 0;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    WMEFirstPageModel *model = _dataArr[indexPath.row];
    self.twoBlock(model);
}
- (void)data
{
    self.contentArr = [NSMutableArray array];
    NSString *strURL = @"http://tingapi.ting.baidu.com/v1/restserver/ting?method=baidu.ting.billboard.billCategory&format=json&from=ios&kflag=1&version=5.5.6&from=ios&channel=appstore&operator=1";
    [WMEAFNetworking GETNetWithUrl:strURL body:nil headerFile:nil response:WMEJOSN success:^(id result) {
        NSArray *tempArr= result[@"content"];
        for (NSDictionary *tempDic in tempArr) {
            NSArray *temp = [WMEFirstPageModel modelHandlerWithArray:tempDic[@"content"]];
            [self.contentArr addObject:temp];
        }
        self.dataArr = [WMEFirstPageModel modelHandlerWithArray:tempArr];
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        NSLog(@"网络解析错误");
    }];
}

@end
